<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="参数类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="dict in product_pattern_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="参数名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入参数名称"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参数状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择"
          clearable
          style="width: 200px"
        >
          <el-option
            v-for="dict in product_pattern_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="search" size="mini" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['yanjing:pattern:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['yanjing:pattern:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['yanjing:pattern:remove']"
          >删除</el-button
        >
      </el-col>
    </el-row>

    <el-table
      v-loading="loading"
      :data="patternList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="序号" type="index" width="60"></el-table-column>
      <!-- <el-table-column label="商品信息" align="center" prop="productId" /> -->
      <el-table-column label="参数类型" align="center" prop="type" width="100">
        <template #default="scope">
          <dict-tag :options="product_pattern_type" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column
        label="参数名称"
        align="center"
        prop="name"
        width="100"
      />
      <el-table-column
        label="是否验证"
        align="center"
        prop="rangeFlag"
        width="100"
      >
        <template #default="scope">
          <dict-tag
            :options="product_pattern_range_flag"
            :value="scope.row.rangeFlag"
          />
        </template>
      </el-table-column>
      <el-table-column label="参数数值" align="center" prop="viewValue" />
      <el-table-column
        label="默认值"
        align="center"
        prop="initValue"
        width="100"
      />
      <el-table-column
        label="参数间隔"
        align="center"
        prop="spaceValue"
        width="100"
      />
      <el-table-column label="参数单位" align="center" prop="unit" width="100" />
      <el-table-column label="参数状态" align="center" prop="status" width="100">
        <template #default="scope">
          <dict-tag
            :options="product_pattern_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="前台显示顺序"
        align="center"
        prop="floor"
        width="120"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="300px"
      >
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="View"
            @click="handleView(scope.row, scope.index)"
            >详细</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['yanjing:pattern:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="minus"
            @click="handleStart(scope.row)"
            v-if="scope.row.status == 'CANCEL'"
            >启用</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="check"
            @click="handleEnd(scope.row)"
            v-else-if="scope.row.status == 'NORMAL'"
            >停用</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yanjing:pattern:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page.sync="queryParams.pageNum"
      v-model:limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-row>
          <el-col :span="10">
            <el-form-item label="参数类型" prop="type" required="true">
              <el-select
                v-model="form.type"
                placeholder="请选择"
                clearable
                style="width: 200px"
              >
                <el-option
                  v-for="dict in product_pattern_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="参数名称" prop="name" required="true">
              <el-input v-model="form.name" placeholder="请输入参数名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="参数状态" prop="status" required="true">
              <el-select
                v-model="form.status"
                placeholder="请选择"
                clearable
                style="width: 200px"
              >
                <el-option
                  v-for="dict in product_pattern_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="开始值" prop="startValue" required="true">
              <el-input v-model="form.startValue" placeholder="请输入开始值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="结束值" prop="endValue" required="true">
              <el-input v-model="form.endValue" placeholder="请输入结束值" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="参数间隔" prop="spaceValue" required="true">
              <el-input
                v-model="form.spaceValue"
                placeholder="请输入参数间隔"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="参数单位" prop="unit" required="true">
              <el-input v-model="form.unit" placeholder="请输入参数单位" />
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="显示顺序" prop="floor" required="true">
              <el-input v-model="form.floor" placeholder="请输入显示顺序" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="是否验证" prop="rangeFlag" required="true">
              <el-select
                v-model="form.rangeFlag"
                placeholder="请选择"
                clearable
                style="width: 200px"
              >
                <el-option
                  v-for="dict in product_pattern_range_flag"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="默认值" prop="initValue" required="true">
              <el-input v-model="form.initValue" placeholder="请输入默认值" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="参数范围" prop="viewValue" required="true">
          <el-input v-model="form.viewValue" placeholder="请输入参数范围" />
        </el-form-item>
        <el-form-item label="验证数据值" prop="rangeValue" align="center">
          <el-input
            v-model="form.rangeValue"
            type="textarea"
            placeholder="请输入默认值"
          />
        </el-form-item>
        <div align="center">
          <p>
            <span style="color: red"
              >*注意事项： 1.验证数据多值,使用分号“;”
              分割。.例如：10;20;30;40;50</span
            >
          </p>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer" align="center">
        <el-button v-if="isDetail" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  queryPageByParam,
  getPattern,
  delPattern,
  addPattern,
  updatePattern,
} from "@/api/product/pattern.js";
import { getCurrentInstance } from "vue";
export default {
  setup() {
    // 获取当前组件实例
    const instance = getCurrentInstance();
    const { product_pattern_type } = instance.proxy.useDict(
      "product_pattern_type"
    );
    const { product_pattern_range_flag } = instance.proxy.useDict(
      "product_pattern_range_flag"
    );
    const { product_pattern_status } = instance.proxy.useDict(
      "product_pattern_status"
    );

    return {
      product_pattern_type,
      product_pattern_range_flag,
      product_pattern_status,
    };
  },
  name: "Pattern",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      patternList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        name: null,
        status: null,
      },
      // 表单参数
      isDetail: true,
      form: {},
      // 表单校验
      rules: {
        type: [
          { required: true, message: "参数类型不能为空", trigger: "blur" },
        ],
        name: [
          { required: true, message: "参数名称不能为空", trigger: "blur" },
        ],
        status: [
          { required: true, message: "参数状态不能为空", trigger: "blur" },
        ],
        startValue: [
          { required: true, message: "开始值不能为空", trigger: "blur" },
        ],
        endValue: [
          { required: true, message: "结束值不能为空", trigger: "blur" },
        ],
        spaceValue: [
          { required: true, message: "参数间隔不能为空", trigger: "blur" },
        ],
        unit: [
          { required: true, message: "参数单位不能为空", trigger: "blur" },
        ],
        floor: [
          { required: true, message: "显示顺序不能为空", trigger: "blur" },
        ],
        rangeFlag: [
          { required: true, message: "是否验证不能为空", trigger: "blur" },
        ],
        initValue: [
          { required: true, message: "默认值不能为空", trigger: "blur" },
        ],
        viewValue: [
          { required: true, message: "参数范围不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      queryPageByParam(this.queryParams).then((response) => {
        this.patternList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.isDetail = true;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        patternId: null,
        productId: null,
        type: null,
        name: null,
        rangeFlag: null,
        rangeValue: null,
        viewValue: null,
        startValue: null,
        endValue: null,
        spaceValue: null,
        initValue: null,
        unit: null,
        level: null,
        status: null,
        floor: null,
        ouputCode: null,
        inputType: null,
        createTime: null,
        createEmployeeId: null,
        updateTime: null,
        updateEmployeeId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.patternId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleView(row) {
      this.open = true;
      this.form = row;
      this.isDetail = false;
      this.title = "参数信息"
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.isDetail = true;
      this.title = "添加参数信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.isDetail = true;
      this.reset();
      const patternId = row.patternId || this.ids;
      getPattern(patternId).then((response) => {
        
        this.form = response.data;
        this.open = true;
        this.title = "修改参数信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.patternId != null) {
            updatePattern(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.level = "SYSTEM";
            addPattern(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 启用 */
    handleStart(row) {
      
      row.status = "NORMAL";
      updatePattern(row).then((response) => {
        this.$modal.msgSuccess("启用成功");
        this.open = false;
        this.getList();
      });
    },
    /** 停用 */
    handleEnd(row) {
      
      row.status = "CANCEL";
      updatePattern(row).then((response) => {
        this.$modal.msgSuccess("停用成功");
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const patternIds = row.patternId || this.ids;
      this.$modal
        .confirm('是否确认删除编号为"' + patternIds + '"的数据项？')
        .then(function () {
          return delPattern(patternIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "yanjing/pattern/export",
        {
          ...this.queryParams,
        },
        `pattern_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
